package com.wzt.consumer.controller;

import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.PageResult;
import com.wzt.consumer.domain.LoginInfo;
import com.wzt.consumer.dto.LoginByAccountDto;
import com.wzt.consumer.dto.SmsCodeLoginDto;
import com.wzt.consumer.dto.SmsCodeRegisterDto;
import com.wzt.consumer.query.LogininfoQuery;
import com.wzt.consumer.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zenith
 */
@RestController
@RequestMapping("/login")
public class LoginInfoController {
    @Autowired
    public ILogininfoService loginInfoService;


    /**
     * 保存或修改
     *
     * @param logininfo 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResponse addOrUpdate(@RequestBody LoginInfo logininfo) {
        try {
            if (logininfo.getId() != null) {
                loginInfoService.editOne(logininfo);
            } else {
                loginInfoService.addOne(logininfo);
            }
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResponse delete(@PathVariable("id") Long id) {
        try {
            loginInfoService.removeOne(id);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResponse delete(@RequestBody List<Long> ids) {
        try {
            loginInfoService.removeMul(ids);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 通过id获取
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResponse get(@PathVariable("id") Long id) {
        try {
            LoginInfo logininfo = loginInfoService.getOne(id);
            return AjaxResponse.oneResponse().setData(logininfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 获取所有
     *
     * @return
     */
    @GetMapping
    public AjaxResponse list() {

        try {
            List<LoginInfo> data = loginInfoService.getAll();
            return AjaxResponse.oneResponse().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageResult 分页对象
     */
    @PostMapping
    public AjaxResponse json(@RequestBody LogininfoQuery query) {
        try {
            PageResult<LoginInfo> pageList = loginInfoService.getPage(query);
            return AjaxResponse.oneResponse().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/account")
    public AjaxResponse loginByAccount(@RequestBody LoginByAccountDto loginDto) {
        System.out.println(loginDto);
        try {
            return loginInfoService.loginByAccount(loginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/sms")
    public AjaxResponse loginBySms(@RequestBody String phone) {
        phone = phone.substring(0, phone.lastIndexOf('='));
        try {
            return loginInfoService.loginBySms(phone);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/sms/validate")
    public AjaxResponse loginBySmsValidate(@RequestBody SmsCodeLoginDto smsCodeDto) {
        try {
            return loginInfoService.smsValidate(smsCodeDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }
}